﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using TierApplication;
using System.Data;
using DevExpress.Web.ASPxEditors;

public partial class System_Order_Order : System.Web.UI.Page
{
    #region private
    T_PRODUCT mProduct = new T_PRODUCT();
    string ProductImageFolder = "..\\..\\ProductImages\\";
    private const string inputQueryStringIDKey = "ORDERID";
    private const string inputQueryStringPageModeKey = "pagemode";
    private const string currentPageModeKey = "CurrentPageMode";
    private const string mDataModel = "mDataModel";

    public enum PageMode
    {
        add,
        update,
        query,
        select
    }
    private Nullable<PageMode> pm;



    #endregion



    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            pm = ViewState[currentPageModeKey] as Nullable<PageMode>;
        }
        else
        {
            #region 权限检查
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            #endregion

            BindSchool();
            BindCancelReason();
            BindOrderStatus();
            SetEnable(false);

            this.txtORDERID.Text = Page.Request.QueryString[inputQueryStringIDKey];

            if (!string.IsNullOrEmpty(this.txtORDERID.Text.Trim()))
            {
                LoadOrder(this.txtORDERID.Text.Trim());
            }
        }

    }

    private void SetEnable(bool isEnable) 
    {
        ddlORDERSTATUS.Enabled = isEnable;
        ddlCANCELREASON.Enabled = isEnable;
        ddlSCHOOLID.Enabled = isEnable;
        txtACCOUNTID.Enabled = isEnable;
        txtACCOUNTNAME.Enabled = isEnable;
        txtCANCELDATE.Enabled = isEnable;
        txtORDERDATE.Enabled = isEnable;
        txtORDERID.Enabled = isEnable;
        txtPAYDATE.Enabled = isEnable;
        txtPAYTYPE.Enabled = isEnable;
        txtTOTALFEE.Enabled = isEnable;
        txtTOTALPOINT.Enabled = isEnable;

         
    }

    private void LoadOrder(String ORDERID)
    {
        ListEditItem theselected;

        T_ORDER mORDER = new T_ORDER();
        mORDER.GetModel(ORDERID);
        ViewState[mDataModel] = mORDER;

        this.txtORDERID.Text = mORDER.ORDERID;
        if (mORDER.ORDERDATE != null)
        {
            this.txtORDERDATE.Text = mORDER.ORDERDATE.Value.ToString("yyyy-MM-dd HH:mm:ss");
        }
        this.txtACCOUNTID.Text = mORDER.ACCOUNTID;

        if (!String.IsNullOrEmpty(mORDER.ORDERSTATUS))
        {
            theselected = ddlORDERSTATUS.Items.FindByValue(mORDER.ORDERSTATUS);
            if (theselected != null)
            {
                ddlORDERSTATUS.SelectedItem = theselected;
            }
        }

        if (mORDER.PAYDATE != null)
        {
            this.txtPAYDATE.Text = mORDER.PAYDATE.Value.ToString("yyyy-MM-dd HH:mm:ss");
        }
        this.txtTOTALFEE.Text = mORDER.TOTALFEE.ToString();
        if (mORDER.CANCELDATE != null)
        {
            this.txtCANCELDATE.Text = mORDER.CANCELDATE.Value.ToString("yyyy-MM-dd HH:mm:ss");
        }

        if (!String.IsNullOrEmpty(mORDER.CANCELREASON))
        {
            theselected = ddlCANCELREASON.Items.FindByValue(mORDER.CANCELREASON);
            if (theselected != null)
            {
                ddlCANCELREASON.SelectedItem = theselected;
            }
        }
        this.txtTOTALPOINT.Text = mORDER.TOTALPOINT.ToString();
        //this.txtREMARK.Text = mORDER.REMARK;
        //this.txtPAYTYPE.Text = mORDER.PAYTYPE;      

        //
        if (mORDER.ACCOUNTID != "")
        { loadAccountInfo(mORDER.ACCOUNTID); }

        loadOrderDetail(ORDERID);
    }

    private void loadAccountInfo(string sAccountID)
    {
        ListEditItem theselected;
        T_ACCOUNT mAccount = new T_ACCOUNT();
        mAccount.GetModel(sAccountID);
        txtACCOUNTNAME.Text = mAccount.ACCOUNTNAME;
        if (!String.IsNullOrEmpty(mAccount.SCHOOLID))
        {
            theselected = ddlSCHOOLID.Items.FindByValue(mAccount.SCHOOLID);
            if (theselected != null)
            {
                ddlSCHOOLID.SelectedItem = theselected;
            }
        }
    }
    private void loadOrderDetail(string ORDERID)
    {
        T_ORDER mOrder = new T_ORDER();
        gridSearchResult.DataSource = mOrder.GetOrderDetailListByOrderID(ORDERID);
        gridSearchResult.DataBind();

        gvOrderCardList.DataSource = mOrder.GetOrderCardListByOrderID(ORDERID);
        gvOrderCardList.DataBind();
    }


    private void BindSchool()
    {
        T_SCHOOL mSchool = new T_SCHOOL();
        DataSet ds = mSchool.GetList("");
        this.ddlSCHOOLID.DataSource = ds;
        this.ddlSCHOOLID.ValueField = "SCHOOLID";
        this.ddlSCHOOLID.TextField = "SCHOOLNAME";
        this.ddlSCHOOLID.DataBind();
        this.ddlSCHOOLID.Items.Insert(0, new ListEditItem("(全部)", ""));
    }

    private void BindCancelReason()
    {
        DataSet ds = P_DDCODE.GetListByType(P_DDCODE.DDType.CancelReason.ToString());
        this.ddlCANCELREASON.DataSource = ds.Tables[0].DefaultView;
        this.ddlCANCELREASON.TextField = "CodeName";
        this.ddlCANCELREASON.ValueField = "CodeID";
        this.ddlCANCELREASON.DataBind();
        this.ddlCANCELREASON.Items.Insert(0, new ListEditItem("(全部)", ""));
    }

    private void BindOrderStatus()
    {
        DataSet ds = P_DDCODE.GetListByType(P_DDCODE.DDType.OrderStatus.ToString());
        this.ddlORDERSTATUS.DataSource = ds.Tables[0].DefaultView;
        this.ddlORDERSTATUS.TextField = "CodeName";
        this.ddlORDERSTATUS.ValueField = "CodeID";
        this.ddlORDERSTATUS.DataBind();
        this.ddlORDERSTATUS.Items.Insert(0, new ListEditItem("(全部)", ""));
    }

    protected void btnProductSave_CallBack(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {


    }

    protected void btnProductSave_Click(object sender, EventArgs e)
    {

    }

}